package team.epos.merchant.service;

import team.epos.merchant.dao.StoreDao;
import team.epos.merchant.dao.UserDao;
import team.epos.utils.jms.ActiveMQutil;
import team.epos.utils.pojo.Advert;
import team.epos.utils.pojo.Apply;
import team.epos.utils.pojo.MUser;
import team.epos.utils.pojo.Store;
import team.epos.utils.properties.ResourcesProperties;
import team.epos.utils.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.TreeMap;

/**
 * Create by Vic Xu on 7/14/2018
 *
 * @author Vic Xu
 */
public class StoreServiceImpl implements StoreService {

    private StoreDao storeDao = new StoreDao();
    private UserDao userDao = new UserDao();

    @Override
    public int openStore(Store store, HttpServletRequest req) {
        if (store.getCardId() == null || !CheckIdUtil.checkId(store.getCardId())) {
            return 0;
        }
        //维持会话
        HttpSession session = req.getSession();
        store.setState(0);
        MUser seUser = (MUser) session.getAttribute("muser");
//        如果没有修改图片，则把上一次保存的图片取出
        if (store.getCardImg() == null) {
            Store store1 = (Store) storeDao.getById(Store.class, store.getId());
            String imgPath = store1.getCardImg();
            store.setCardImg(imgPath);
        }
        String path = req.getSession().getServletContext().getRealPath(store.getCardImg());
        String base64 = FileUploadUtil.img2Base64(path);
        Apply apply = new Apply(store.getCardId(), base64,
                store.getName(), store.getStoreName(), store.getStoreAddr(), store.getBusinessId());
        String json = JsonUtil.obJectToJson(apply);
        ActiveMQutil activeMQutil = ActiveMQutil.getActiveMQutil();
        String jmsUser = ResourcesUtil.resourcesProperties.getJmsUser();
        String jmsPassWord = ResourcesUtil.resourcesProperties.getPassWord();
        String jmsUrl = ResourcesUtil.resourcesProperties.getJmsUrl();
        String jmsQueue = ResourcesUtil.resourcesProperties.getQueue();
        activeMQutil.setConnectionFactory(jmsUser, jmsPassWord, jmsUrl);
        activeMQutil.setProducerP2p(jmsQueue, json);
        int i = 0;
        if (store.getId() != null) {
            i = storeDao.update(store);
        } else {
            i = storeDao.insert(store, "seqStore");
            if (i > 0) {
                MUser user;
                List<Store> storel;
                TreeMap<String, Object> params = new TreeMap<>();
                params.put("cardId", store.getCardId());
                storel = (List<Store>) storeDao.getByCondition(Store.class, params);
                user = (MUser) userDao.getById(MUser.class, seUser.getId());
                user.setsId(storel.get(0).getId());
                i = userDao.update(user);
            }
        }
        return i;
    }

    @Override
    public Store recommend(String sId) {
        Store store = (Store) storeDao.getById(Store.class, Integer.parseInt(sId));
        return store;
    }

    @Override
    public void subRecommend(Advert advert, HttpServletRequest req) {
        String path = req.getSession().getServletContext().getRealPath(advert.getImg());
        advert.setImg(FileUploadUtil.img2Base64(path));
        String json = JsonUtil.obJectToJson(advert);
        String jmsUser = ResourcesUtil.resourcesProperties.getJmsUser();
        String jmsPassWord = ResourcesUtil.resourcesProperties.getPassWord();
        String jmsUrl = ResourcesUtil.resourcesProperties.getJmsUrl();
        String jmsQueue = ResourcesUtil.resourcesProperties.getAqueue();
        ActiveMQutil activeMQutil = ActiveMQutil.getActiveMQutil();
        activeMQutil.setConnectionFactory(jmsUser, jmsPassWord, jmsUrl);
        activeMQutil.setProducerP2p(jmsQueue, json);
    }

    @Override
    public String getState(int sId) {
        Store store = (Store) storeDao.getById(Store.class, sId);
        String cardId = store.getCardId();
        String jsonStr = "";
        String httpurl=ResourcesUtil.resourcesProperties.getHttpurl();
        try {
            jsonStr = HttpRequestorUtil.doGet(httpurl + cardId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    @Override
    public Store rejectApply(int sId) {
        Store store = (Store) storeDao.getById(Store.class, sId);
        return store;
    }


}
